#include "auctrowwidget.h"

AuctRowWidget::AuctRowWidget(QMap<QString, QStringList> opened_album_attrs_)
{
this->setAttribute(Qt::WA_DeleteOnClose);
setupUi(this);
readSettings();

opened_album_attrs = opened_album_attrs_;
current_record = QSqlRecord();
current_index = QModelIndex();
is_new = false;

QLineEdit * auctLE = qobject_cast<QLineEdit *>(auctComboBox->lineEdit());
auctLE->setMaxLength(128);
auctComboBox->addItems( opened_album_attrs.value("auctions") );
auctComboBox->setCurrentIndex(-1);

QLineEdit * condLE = qobject_cast<QLineEdit *>(conditionComboBox->lineEdit());
condLE->setMaxLength(128);
conditionComboBox->addItems( opened_album_attrs.value("conditions") );
conditionComboBox->setCurrentIndex(-1);

QLineEdit * currLE = qobject_cast<QLineEdit *>(currComboBox->lineEdit());
currLE->setMaxLength(32);
currComboBox->addItems( opened_album_attrs.value("currencies") );
currComboBox->setCurrentIndex(-1);

dateEdit->setDate( QDate::currentDate() );

QObject::connect(commitButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(save_clicked()));
QObject::connect(cancelButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(close()));

}

AuctRowWidget::~AuctRowWidget()
{
}

void AuctRowWidget::set_record( QSqlRecord record,
                                QModelIndex index )
{
    if (!record.isNull("AUCT_NAME") )
    {
    QString s = record.value("AUCT_NAME").toString();
    auctNameCheckBox->setChecked(true);
    auctComboBox->setEditText(s);
    }
    else
    {
    auctNameCheckBox->setChecked(false);
    auctComboBox->clearEditText();
    }

    if (!record.isNull("AUCT_DATE") )
    {
    QDate date = QDate::fromString( QString::number( record.value("AUCT_DATE").toInt() ),
                                    "yyyyMMdd" );
        if ( true == date.isValid() )
        {
        auctDateCheckBox->setChecked(true);
        dateEdit->setDate(date);
        }
    }
    else
    {
    auctDateCheckBox->setChecked(false);
    }

    if (!record.isNull("AUCT_NOTE") )
    {
    QString s = record.value("AUCT_NOTE").toString();
    auctNoteCheckBox->setChecked(true);
    auctLineEdit->setText(s);
    }
    else
    {
    auctNoteCheckBox->setChecked(false);
    auctLineEdit->clear();
    }

    if (!record.isNull("AUCT_COND") )
    {
    QString s = record.value("AUCT_COND").toString();
    conditionCheckBox->setChecked(true);
    conditionComboBox->setEditText(s);
    }
    else
    {
    conditionCheckBox->setChecked(false);
    conditionComboBox->clearEditText();
    }

    if (!record.isNull("AUCT_PR") )
    {
    double i = record.value("AUCT_PR").toDouble();
    priceCheckBox->setChecked(true);
    prDoubleSpinBox->setValue(i);    
    }
    else
    {
    priceCheckBox->setChecked(false);
    }

    if (!record.isNull("AUCT_CURR") )
    {
    QString s = record.value("AUCT_CURR").toString();
    currCheckBox->setChecked(true);
    currComboBox->setEditText(s);
    }
    else
    {
    currCheckBox->setChecked(false);
    currComboBox->clearEditText();
    }

current_record = QSqlRecord(record);
current_index = QModelIndex(index);
}

QSqlRecord AuctRowWidget::read_record(QSqlRecord record)
{
    if ( auctNameCheckBox->isChecked() )
    {
    record.setValue( "AUCT_NAME", QVariant( auctComboBox->currentText() ) );
    }
    else
    {
    record.setNull("AUCT_NAME");
    }

    if ( conditionCheckBox->isChecked() )
    {
    record.setValue("AUCT_COND", QVariant( conditionComboBox->currentText() ) );    }
    else
    {
    record.setNull("AUCT_COND");
    }

    if ( auctNoteCheckBox->isChecked() )
    {
    record.setValue("AUCT_NOTE", QVariant( auctLineEdit->text() ) );
    }
    else
    {
    record.setNull("AUCT_NOTE");
    }

    if ( priceCheckBox->isChecked() )
    {
    record.setValue("AUCT_PR", QVariant( prDoubleSpinBox->value() ) );
    }
    else
    {
    record.setNull("AUCT_PR");
    }

    if ( currCheckBox->isChecked() )
    {
    record.setValue("AUCT_CURR", QVariant( currComboBox->currentText() ) );
    }
    else
    {
    record.setNull("AUCT_CURR");
    }

    if ( auctDateCheckBox->isChecked() )
    {
    QDate d = dateEdit->date();
    QString d_str = d.toString("yyyyMMdd");
    bool ok = false;
    int d_int = d_str.toInt( &ok, 10 );
        if ( true == ok )
        {
        record.setValue("AUCT_DATE", d_int );
        }
        else
        {
        record.setNull("AUCT_DATE");
        }
    }
    else
    {
    record.setNull("AUCT_DATE");
    }

return record;
}


void AuctRowWidget::new_record( qint64 coin_id, QString opened_album )
{

    if ( true == opened_album.isEmpty() )
    {
    return;
    }

QSqlRecord record = QSqlRecord();

qint64 auct_id = Q_INT64_C(-1);
    {
    QSqlDatabase db = QSqlDatabase::database("collection");
       if ( !db.isOpen() )
       {
       return;
       }
    QString s = QLatin1String("select gen_id(");
    s.append(opened_album);
    s.append(QLatin1String("_auctions_gen,1) from rdb$database"));
    QSqlQuery query(s, db);
        while (query.next())
        {
        auct_id = query.value(0).toLongLong();
        }
    query.clear();
    }

    if (auct_id == -1)
    {
    return;
    }

is_new = true;

record.insert( 0, QSqlField("AUCT_ID", QVariant::LongLong) );
record.setValue("AUCT_ID", auct_id);

record.insert( 1, QSqlField("COIN_ID", QVariant::LongLong) );
record.setValue("COIN_ID", coin_id);

record.insert(  2, QSqlField("AUCT_NAME", QVariant::String) );
record.insert(  3, QSqlField("AUCT_DATE", QVariant::Int) );
record.insert(  4, QSqlField("AUCT_NOTE", QVariant::String) );
record.insert(  5, QSqlField("AUCT_COND", QVariant::String) );
record.insert(  6, QSqlField("AUCT_PR", QVariant::Double) );
record.insert(  7, QSqlField("AUCT_CURR", QVariant::String) );
record.insert(  8, QSqlField("RESERVED0", QVariant::String) );

current_record = QSqlRecord(record);
}

void AuctRowWidget::save_clicked()
{
QSqlRecord record = read_record(current_record);
emit record_to_commit( record,
                       current_index,
                       QLatin1String("AUCT_ID"),
                       is_new);
close();
}

void AuctRowWidget::writeSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);
settings.beginGroup( QLatin1String("AuctRowWidget") );
settings.setValue( QLatin1String("size"), QVariant( this->size() ) );
settings.setValue( QLatin1String("pos"), QVariant( this->pos() ) );
settings.endGroup();
}

void AuctRowWidget::readSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);                
settings.beginGroup( QLatin1String("AuctRowWidget") );
resize( settings.value( QLatin1String("size"), QSize( 400, 310 ) ).toSize() );
move( settings.value( QLatin1String("pos"), QPoint( 0, 0 ) ).toPoint() );
settings.endGroup();
}

void AuctRowWidget::closeEvent( QCloseEvent * e )
{
writeSettings();
e->accept();
}

